package com.game.raceroom.player.effect
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.getTimer;
	
	import com.util.SwfUtil;
	import com.game.raceroom.Room;
	public class AddedItemEffect extends Effect
	{   
         var x,y:Number;
         var room:Room;
         var itemid:int;
		 public function AddedItemEffect(room:Room,itemid:int,x:Number,y:Number)
		 {  
		 	this.room=room;
            this.itemid=itemid;
            this.x=x;
            this.y=y;  
			this.effectType=EFFECT_TYPE_ADDEDITEM;
			startTime=getTimer();
			maxTime=15000;
			loadMovie();
		 }

         private var loadOk:Boolean=false;
         var contDown:Boolean=false;
         var time:Number;
         var updown:Boolean=true;
         
         public override function update():Boolean{
        	if(loadOk) {
        		
        		

            mc.x=x-room.mapContrl.mapDisp.screen_start_x-mc.width/2;
            mc.y=y-room.mapContrl.mapDisp.screen_start_y-mc.height/2;
                mc.visible=true;
                time=getTimer()-this.startTime;
	            if(time>=maxTime){
	                mc.alpha-=0.1;
	                if(mc.alpha<=0){
		            	room.bgCanvas.removeChild(mc);
		            	mc.stop();
		            	this.valid=false;
	                }
	            }else{
	            	if(maxTime-time<=2000){
	            		contDown=true;
	            	}
		            if(contDown){
			            if(updown){
			            	mc.alpha-=0.1;
			            	if(mc.alpha<=0.5){
			            		mc.alpha=0.5;
			            		updown=false;
			            	}
			            }else{
			            	mc.alpha+=0.1;
			            	if(mc.alpha>=1){
			            		mc.alpha=1;
			            		updown=true;
			            	}	
			            }
		            }
	            }

	        }
 

          

            return true;
         }
         public override function destroy():Boolean{
         	if(!loadOk) return true;
            room.bgCanvas.removeChild(mc);
            mc.stop();
		    this.valid=false;

            return true;
         }  
        protected var myLoader:Loader;
		public function loadMovie():void{
			myLoader=new Loader();
		    myLoader.contentLoaderInfo.addEventListener(
  	        Event.COMPLETE,function (e:Event):void{initEftOk(e)});
            myLoader.loadBytes(SwfUtil.getEffectAddedtemStream(this.itemid));
      
		}
		private var mc:MovieClip;
        public function initEftOk(e:Event):void{
            mc = myLoader.content as MovieClip; 
             mc.visible=false;
        	 
        			mc.scaleX=0.8;
        			mc.scaley=0.8;
        	 
             mc.play();
			room.bgCanvas.addChild(mc);
			loadOk=true;

        } 
	}
}